'use strict';
app.service('goodsService',['$q','utilService',function($q,utilService){
    var _this_=this;

    //查询品牌信息
    _this_.querybrands= function ($scope) {
        var deferred = $q.defer();

        utilService.get({
            url:$scope.app.domain + 'chain/brand/alls'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })

        return deferred.promise;

    }

    //查询商品分类
    _this_.queryType= function ($scope) {
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'chain/type/goods/type'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        })
        return deferred.promise;
    }

    //批量上架
    _this_.batchUpdateUpStatus= function ($scope) {
        var updateSelect=new Array();
        for(var i=0;i<$scope.selected.length;i++){
            if($scope.selected[i].status==0 || $scope.selected[i].status==4){
                updateSelect.push($scope.selected[i].id);
            }
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/goods/batch/status',
            data:{ids:updateSelect,status:1},
            method:"PUT"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    //批量下架
    _this_.batchUpdateDownStatus= function ($scope) {
        var updateSelect=new Array();
        for(var i=0;i<$scope.selected.length;i++){
            if($scope.selected[i].status==1){
                updateSelect.push($scope.selected[i].id);
            }
        }

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/goods/batch/status',
            data:{ids:updateSelect,status:0},
            method:"PUT"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    //更新
    _this_.updateStatus= function ($scope,id,status) {

        if(status==2){
            return;
        }

        status=(status==3)?2:(status==0||status==4)?1:0;
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/goods/status',
            data:{id:id,status:status},
            method:"PUT"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }


    //删除商品
    _this_.delete=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'chain/goods/'+$scope.goods.id,
            method:"DELETE"
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

}])